//
//  STMessageComposeViewController.swift
//  iPhone
//
//  Created by Toj on 3/28/23.
//

import UIKit
import MessageUI

// MARK: - 懒加载(属性), 变量, 初始化init
class STMessageComposeViewController: UIViewController {
    
    /* 弹出Message */
    private lazy var messageButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .white
        view.setTitle("弹出短信", for: .normal)
        view.setTitleColor(.red, for: .normal)
        view.frame = CGRect(origin: .zero, size: CGSize(width: 150, height: 44))
        view.addTarget(self, action: #selector(didtapMessage), for: .touchUpInside)
        return view
    }()
}

// MARK: - 生命周期
extension STMessageComposeViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
}

// MARK: - 点击事件
private extension STMessageComposeViewController {
    
    @objc func didtapMessage() {
        guard MFMessageComposeViewController.canSendText() else { return }
        
        let controller = MFMessageComposeViewController()
        controller.messageComposeDelegate = self
        controller.body =  "34567"
        
        present(controller, animated: true, completion: nil)
    }
}

extension STMessageComposeViewController: MFMessageComposeViewControllerDelegate {
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true) { [weak self] in
            guard let _ = self else { return }
        }
    }
}

// MARK: - 初始化设置
private extension STMessageComposeViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(messageButton)
    }
    
    func setupMakeLayoutSubviews() {
        let size = messageButton.frame.size
        messageButton.snp.remakeConstraints { make in
            make.center.equalTo(view)
            make.size.equalTo(size)
        }
    }
    
    func setupMakeInitialize() {
        
    }
}
